<!-- 考试列表 -->
<template>
  <div class="app-container">
    <!-- 搜索工具栏 -->
    <div class="toolbar">
      <el-form :inline="true" :model="queryParams" class="search-form">
        <el-form-item label="试卷名称">
          <el-input
            v-model="queryParams.keyword"
            placeholder="试卷名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="科目">
          <el-input
            v-model="queryParams.subject"
            placeholder="科目"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <el-icon><Search /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetQuery">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 考试列表 -->
    <el-table v-loading="loading" :data="paperList" style="width: 100%">
      <el-table-column
        prop="paperName"
        label="试卷名称"
        show-overflow-tooltip
      />
      <el-table-column prop="subject" label="科目" width="120" />
      <el-table-column prop="totalScore" label="总分" width="80" />
      <el-table-column prop="duration" label="时长(分钟)" width="100" />
      <el-table-column prop="status" label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="getStatusType(row.status)">
            {{ getStatusText(row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right">
        <template #default="{ row }">
          <el-button
            type="primary"
            link
            :disabled="row.status !== 0"
            @click="handleStartExam(row)"
          >
            开始考试
          </el-button>
          <el-button
            v-if="row.status === 1"
            type="success"
            link
            @click="handleViewRecord(row)"
          >
            查看成绩
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination">
      <el-pagination
        v-model:current-page="queryParams.pageNum"
        v-model:page-size="queryParams.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 开始考试确认对话框 -->
    <el-dialog
      title="考试须知"
      v-model="dialogVisible"
      width="500px"
      :close-on-click-modal="false"
    >
      <div class="exam-notice">
        <h4>请仔细阅读以下考试须知：</h4>
        <ol>
          <li>
            考试时间为 {{ currentPaper.duration }} 分钟，请合理安排答题时间。
          </li>
          <li>考试过程中请勿刷新页面或关闭浏览器，否则可能导致答案丢失。</li>
          <li>考试时间结束后，系统将自动提交试卷。</li>
          <li>提交试卷后将无法修改答案。</li>
        </ol>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="startExam">开始考试</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";
import { getPapers } from "@/api/exam";

const router = useRouter();

// 查询参数
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  keyword: "",
  subject: "",
});

// 考试列表数据
const loading = ref(false);
const paperList = ref([]);
const total = ref(0);

// 获取考试列表
const getPaperList = async () => {
  loading.value = true;
  try {
    const { records, total: totalCount } = await getPapers(queryParams);
    paperList.value = records;
    total.value = totalCount;
  } catch (error) {
    console.error("获取考试列表失败：", error);
  } finally {
    loading.value = false;
  }
};

// 查询按钮点击
const handleQuery = () => {
  queryParams.pageNum = 1;
  getPaperList();
};

// 重置按钮点击
const resetQuery = () => {
  queryParams.keyword = "";
  queryParams.subject = "";
  handleQuery();
};

// 分页大小改变
const handleSizeChange = (val) => {
  queryParams.pageSize = val;
  getPaperList();
};

// 页码改变
const handleCurrentChange = (val) => {
  queryParams.pageNum = val;
  getPaperList();
};

// 获取状态标签类型
const getStatusType = (status) => {
  switch (status) {
    case 0:
      return "primary";
    case 1:
      return "success";
    default:
      return "info";
  }
};

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 0:
      return "未开始";
    case 1:
      return "已完成";
    default:
      return "未知";
  }
};

// 开始考试相关
const dialogVisible = ref(false);
const currentPaper = ref({});

// 开始考试按钮点击
const handleStartExam = (row) => {
  currentPaper.value = row;
  dialogVisible.value = true;
};

// 开始考试
const startExam = () => {
  dialogVisible.value = false;
  router.push(`/exam/taking/${currentPaper.value.id}/${currentPaper.value.duration}/${currentPaper.value.totalScore}/${currentPaper.value.paperName}`);
};

// 查看成绩按钮点击
const handleViewRecord = (row) => {
  router.push("/record");
};

onMounted(() => {
  getPaperList();
});
</script>

<style scoped>
.toolbar {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.search-form {
  flex: 1;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.exam-notice {
  color: #606266;
  line-height: 1.8;
}

.exam-notice h4 {
  margin-top: 0;
  margin-bottom: 15px;
  color: #303133;
}

.exam-notice ol {
  margin: 0;
  padding-left: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
